package vip.aster.workflow.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import vip.aster.common.utils.PageInfo;
import vip.aster.common.utils.ResultInfo;
import vip.aster.framework.log.annotation.Log;
import vip.aster.framework.log.enums.BusinessTypeEnum;
import vip.aster.workflow.entity.WfApp;
import vip.aster.workflow.query.WfAppQuery;
import vip.aster.workflow.service.WfAppService;
import vip.aster.workflow.vo.WfAppVO;

import java.util.List;

/**
 * 应用管理
 *
 * @author Aster lipian1004@163.com
 * @since 2024-05-11 09:48:46
 */
@Tag(name = "应用管理")
@RestController
@RequestMapping("/workflow/app")
@AllArgsConstructor
public class WfAppController {

    /**
     * 应用管理service
     */
    private final WfAppService wfAppService;

    @GetMapping("/page")
    @Operation(summary = "分页查询应用")
    @Log(type = BusinessTypeEnum.SEARCH)
    @PreAuthorize("hasAuthority('wf:app:list')")
    public ResultInfo<PageInfo<WfAppVO>> page(@ParameterObject @Valid WfAppQuery query) {
        PageInfo<WfAppVO> page = wfAppService.pageList(query);
        return ResultInfo.success(page);
    }

    @GetMapping("/list")
    @Operation(summary = "查询应用集合")
    @Log(type = BusinessTypeEnum.SEARCH)
    @PreAuthorize("hasAuthority('wf:app:list')")
    public ResultInfo<List<WfAppVO>> list(@ParameterObject @Valid WfAppQuery query) {
        List<WfAppVO> list = wfAppService.list(query);
        return ResultInfo.success(list);
    }

    @GetMapping("/info/{id}")
    @Operation(summary = "详情")
    @Log(type = BusinessTypeEnum.SEARCH)
    @PreAuthorize("hasAuthority('wf:app:info')")
    public ResultInfo<WfAppVO> get(@PathVariable("id") String id) {
        WfApp entity = wfAppService.getById(id);
        return ResultInfo.success(new WfAppVO(entity));
    }

    @PostMapping("/save")
    @Operation(summary = "保存")
    @Log(type = BusinessTypeEnum.SAVE)
    @PreAuthorize("hasAnyAuthority('wf:app:add','wf:app:edit')")
    public ResultInfo<String> save(@RequestBody WfAppVO vo) {
        wfAppService.save(vo);
        return ResultInfo.success();
    }

    @PostMapping("/delete")
    @Operation(summary = "删除")
    @Log(type = BusinessTypeEnum.DELETE)
    @PreAuthorize("hasAuthority('wf:app:delete')")
    public ResultInfo<String> delete(@RequestBody List<String> idList) {
        wfAppService.removeBatchByIds(idList);
        return ResultInfo.success();
    }
}
